<?php
/**
 * Created by PhpStorm.
 * User: liuxiaojie
 * Date: 2019-05-15
 * Time: 16:58
 */

namespace app\ops\controller;

use app\model\ServiceConfig;
use app\tools\Wx;

class Orders extends Base
{

    function index()
    {
        $where = [
            'o.status'=> ['>', 10]
        ];

        $list = db('repair_order')
            ->alias('o')
            ->join('accept_order a', 'o.id = a.o_id', 'left')
            ->join('customer c', 'c.id = o.customer_id')
            ->where($where)
            ->field('o.id, o.order_no, o.status, o.images as cover, o.pay_money, o.add_time, a.name, a.status as a_status, a.star, o.sheet_type')
            ->order('o.id desc')
            ->paginate(10);

        $this->assign('list', $list);
        return $this->fetch();
    }

    function detail()
    {
        $where = [
            'o.id'=> request()->param('id')
        ];
        $data = db('repair_order')
            ->alias('o')
            ->join('accept_order a', 'o.id = a.o_id', 'left')
            ->join('customer c', 'c.id = o.customer_id')
            ->where($where)
            ->field('o.id, o.order_no, o.status, o.img_arr, o.details, o.pay_money, o.add_time, o.mobile, a.mobile as a_mobile, a.name, a.status as a_status, a.star, o.address, o.type')
            ->find();

        $service = db('service_config')->where([])->field('id, name')->select();

        $data['img_arr'] = explode(',', $data['img_arr']);
        $this->assign('data', $data);
        $this->assign('service', array_column($service, 'name', 'id'));
        return $this->fetch();
    }

    function edit($id){

        if($post = request()->post()){
            db('repair_order')->update($post);
            if($post['sheet_type'] == 1)
            {
                $wx = new Wx();
                $unionid = db('customer')->where(['id'=> $post['master_id']])->value('unionid');
                $openid = db('customer')->where(['unionid'=> $unionid, 'type'=> 2])->value('openid');
                $data = db('repair_order')->where(['id'=> $post['id']])->field('id, details, order_no, add_time')->find();
                $wx->sendTemplate([
                    'openid'=> $openid,
                    'template_id'=> 'VkOPusrnpM5ueEl1WCcS9Pf7CBghPYMmWtkSrsCn9Ao',
                    'page'=> 'pages/repair/worker/order/order',
                    'data'=>[
                        "first"=> [
                            "value"=> $data['details'],
                        ],
                        "keyword1"=> [
                            "value"=> $data['order_no'],
                        ],
                        "keyword2"=> [
                            "value"=> $data['add_time'],
                        ],
                    ]
                ]);

                db('repair_order')->where(['id'=> $data['id']])->update(['is_tui'=> 1]);
            }
            return success([]);
        }else{
            $data = db('repair_order')->where(['id'=> $id])->field("id, sheet_type, master_id")->find();
//            dump($data);
            $this->assign('data', $data);
            return $this->fetch();
        }

    }



}